'''
Created on Jan 8, 2011

@author: dexen
'''

import os
import random

import common

GEN_SIZE = 4
MUTATION_PROB = 1/float(GEN_SIZE*4.0)
#INITIALISATION 
#function for generating genotype
def random_genotype(size):
    genotype = []
    for _ in range(size):
        random.seed()
        genotype.append(random.random())
    return genotype

#houdini geometry
def develop(genotype, basefilepath, filename):
    common.enableHouModule()
    print "after enableHouModule"
    import hou
    print "after import hou"
    basefile = os.path.join(basefilepath, filename)
    print "after basefile", basefile
    hou.hipFile.load(basefile)
    print "after hipFile.load"
    gen_node = hou.node("/obj/geo1/genotype")
    print "after hou.node()"
    for cnt in range(len(genotype)):
        gen_node.setParms({"genotype"+str(cnt+1):genotype[cnt]})
        
    phenotype_geom = os.path.join(basefilepath, "phenotype")
    if not os.path.isdir(phenotype_geom):
        os.mkdir(phenotype_geom)
    file_path = os.path.join(phenotype_geom, "eval.bgeo")
    hou.node("/obj/geo1/phenotype").geometry().saveToFile(file_path)
    
def sim_ep(filename):
    #_write_phenotype(phenotype)
    common.enableHouModule()
    import hou
    basefile = os.path.join(common.HOUDINI_BASE_FILE, filename)
    hou.hipFile.load(basefile)
    print "I am after loading basefile"
    hou.node("/obj/geo1/OUT").cook()
    print "I am after cooking"
    node = hou.node("/obj/geo1/OUT")
    prim = node.geometry().prims()[0]
    att = prim.attribValue("TotalEnergyTransfer [KWh]")
    return att 

def sim_area(filename):
    #_write_phenotype(phenotype)
    common.enableHouModule()
    import hou
    basefile = os.path.join(common.HOUDINI_BASE_FILE, filename)
    hou.hipFile.load(basefile)
    hou.node("/obj/geo1/OUT").cook()
    node = hou.node("/obj/geo1/OUT")
    prims = node.geometry().prims()
    area_list = []
    for prim in prims:
        area = prim.attribValue("area")
        area_list.append(area)
    total = sum(area_list)
    return total

def write_results(results, basefilepath):
    result_filepath = os.path.join(basefilepath, "overallresult")
    if not os.path.isdir(result_filepath):
        os.mkdir(result_filepath)
    prevdir = os.getcwd()
    os.chdir(result_filepath)
    result_file = open("finalresult.csv",  "w")
    #write the results into a csv file 
    result_file.write("total_energy_transfer(kWh)(daily_avg),area\n")
    result_file.write(results)
    result_file.close()
    os.chdir(prevdir)
    

def main():
    results = ""
    for _ in range(5):
        genotype = random_genotype(GEN_SIZE)
        print "Before develop"
        develop(genotype, common.HOUDINI_BASE_FILE, "dev.hipnc")
        print "After develop"
        score_energy = sim_ep("eval_energy.hipnc")
        score_area = sim_area("eval_area.hipnc")
        results += "{0},{1}\n".format(score_energy, score_area)
        write_results(results, common.HOUDINI_BASE_FILE)

if __name__ == "__main__":
    main()